package com.ruoyi.develop.orderIronGoods.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsCustomer.domain.GoodsCustomer;
import com.ruoyi.develop.project.domain.Project;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import org.apache.poi.ss.usermodel.DateUtil;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.orderIronGoods.mapper.OrderIronGoodsMapper;
import com.ruoyi.develop.orderIronGoods.domain.OrderIronGoods;
import com.ruoyi.develop.orderIronGoods.service.IOrderIronGoodsService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 铁件物料编码Service业务层处理
 *
 * @author lmm
 * @date 2025-06-06
 */
@Service
public class OrderIronGoodsServiceImpl implements IOrderIronGoodsService {
    @Resource
    private OrderIronGoodsMapper orderIronGoodsMapper;
    @Value(value = "${ruoyi.path}")
    public String basePath;
    @Value(value = "${ruoyi.templatePath}")
    public String templatePath;
    @Value(value = "${ruoyi.templateImport}")
    public String templateImport;

    /**
     * 查询铁件物料编码
     *
     * @param id 铁件物料编码主键
     * @return 铁件物料编码
     */
    @Override
    public OrderIronGoods selectById(String id) {
        return orderIronGoodsMapper.selectById(id);
    }

    /**
     * 查询铁件物料编码列表
     *
     * @param entity 铁件物料编码
     * @return 铁件物料编码
     */
    @Override
    public List<OrderIronGoods> selectList(OrderIronGoods entity) {
        return orderIronGoodsMapper.selectList(entity);
    }

    /**
     * 新增铁件物料编码
     *
     * @param entity 铁件物料编码
     * @return 结果
     */
    @Override
    public int insert(OrderIronGoods entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return orderIronGoodsMapper.insert(entity);
    }

    /**
     * 修改铁件物料编码
     *
     * @param entity 铁件物料编码
     * @return 结果
     */
    @Override
    public int update(OrderIronGoods entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return orderIronGoodsMapper.update(entity);
    }

    /**
     * 批量删除铁件物料编码
     *
     * @param ids 需要删除的铁件物料编码主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return orderIronGoodsMapper.deleteByIds(ids);
    }

    /**
     * 删除铁件物料编码信息
     *
     * @param id 铁件物料编码主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return orderIronGoodsMapper.deleteById(id);
    }

    @Override
    public Map<String, Object> importData(OrderIronGoods entity, MultipartFile file) {
        String tmplName = "orderIronGoods.xml";
        String errortext = "";
        Map<String, Object> map = new HashMap<>();
        int i = 0;
        try {
            InputStream in = new FileInputStream(basePath + File.separator
                    + templatePath + File.separator + templateImport + File.separator + tmplName);
            XLSReader mainReader = ReaderBuilder.buildFromXML(in);
            List<OrderIronGoods> list = new ArrayList<>();
            Map beans = new HashMap();
            beans.put("list", list);
            XLSReadStatus readStatus = mainReader.read(file.getInputStream(), beans);
            if (readStatus.isStatusOK()) {
                for (OrderIronGoods orderIronGoods : list) {
                    insert(orderIronGoods);
                    i++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("errorText", errortext);
        map.put("insertCount", i);
        return map;
    }
}
